<?php
/**
 * Description: 主页
 *
 * @author      Eden
 * @datetime    2016/12/21 20:35
 * @copyright   Beijing CmsTop Technology Co.,Ltd.
 */

class Home extends WAP_Controller
{

    private $slideNum = 4;

    private $width = 640;
    private $height = 360;

    public function __construct()
    {
        parent::__construct();
        try {
            $config = $this->_getConfigByKey('general', 'value');
            if (!empty($config) && !empty($config['value'])) {
                $config = json_decode($config['value'], true);
                $this->slideNum = !empty($config['slideNum']) ? intval($config['slideNum']) : $this->slideNum;
            }
        } catch(Exception $e){
            $this->output->send(array(
                'state' => false,
                'error' => '配置错误'
            ));
        }
    }

    public function index()
    {
        $slide = $this->_getSlide();

        $lists = $this->_getLists();


        $this->load->view('wap/goods/index.html', array(
            'slide' => empty($slide) ? [] : $slide,
            'lists' => empty($lists) ? [] : $lists
        ));
    }

    private function _getLists($params = array())
    {
        $db = clone($this->db);
        $q = $db->select('id, goodsName, thumb, expressWeight, streamId, price, created')
            ->from('base_goods')
            ->order_by('modified', 'DESC')
            ->limit(10)
            ->order_by('created', 'DESC')
            ->where(array('status' => 1))
            ->where("stock > 0")
            ->get();
        $rows = $q->result_array();
        if (empty($rows)){
            return [];
        }

        $results = $goodsIds = [];
        foreach($rows as $item) {
            array_push($goodsIds, $item['id']);
            $results[] = array(
                'goodsId' => intval($item['id']),
                'goodsName' => value($item, 'goodsName'),
                'thumb' => thumb($item['thumb'], $this->width),
                'price' => doubleval($item['price']),
                'expressWeight' => doubleval($item['expressWeight']),
                'created' => formateTimeStamp($item['created']),
                'isCollected' => false,
                'isAddToCar' => false,
                'isLive' => !empty($item['streamId']),
            );
        }

        return $results;
    }

    /**
     * 请求幻灯列表
     */
    private function _getSlide()
    {
        $db = clone($this->db);
        $q = $db->select('b.id, b.name, b.thumb, b.streamId, b.created')
            ->limit($this->slideNum)
            ->order_by('modified', 'DESC')
            ->order_by('created', 'DESC')
            ->where('status', 1)
            ->from('base as b')
            ->get();
        $rows = $q->result_array();
        $return = array();
        if (!empty($rows)) foreach ($rows as $item) {
            $return[] = array(
                'baseId' => intval($item['id']),
                'baseName' => value($item, 'name'),
                'thumb' => thumb($item['thumb'], $this->width, $this->height),
                'created' => formateTimeStamp($item['created']),
                'isLive' => !empty($item['streamId'])
            );
        } else {
            return [];
        }

        return $return;
    }

}

